#include "stm32f10x.h"                  // 设备头文件
#include "serial.h"                    // 串口头文件

uint8_t serial_rxdata;                 // 串口接收到的数据
uint8_t serial_rxflag;                 // 串口接收完成标志

uint16_t Red_x;                      // 红激光在OpenMV的坐标
uint16_t Red_y;
uint16_t Green_x = 86;                    // 绿激光在OpenMV的坐标
uint16_t Green_y = 66;

void serial_init(void)
{
    // 初始化USART1和GPIOA的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    GPIO_InitTypeDef GPIO_InitStructure; // GPIO初始化结构体
    // 将PA9配置为推挽复用输出模式，用于USART1的TX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // 将PA10配置为上拉输入模式，用于USART1的RX
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    USART_InitTypeDef USART_InitStructure; // USART初始化结构体
    // 设置USART1的参数：波特率9600，无硬件流控，模式为发送和接收，无校验位，1个停止位，8位数据长度
    USART_InitStructure.USART_BaudRate = 9600;
    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
    USART_InitStructure.USART_Parity = USART_Parity_No;
    USART_InitStructure.USART_StopBits = USART_StopBits_1;
    USART_InitStructure.USART_WordLength = USART_WordLength_8b;
    USART_Init(USART1, &USART_InitStructure);

    // 使能USART1的接收中断
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

    // 设置NVIC优先级分组
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

    NVIC_InitTypeDef NVIC_InitStructure; // NVIC初始化结构体
    // 设置USART1中断的优先级
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);

    // 使能USART1
    USART_Cmd(USART1,DISABLE);
}

void Serial_SendByte(uint8_t Byte)
{
    // 通过USART1发送一个字节的数据
    USART_SendData(USART1, Byte);
    // 等待发送数据寄存器为空
    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}

void serial_receivebyte(uint8_t BYTE)
{
    // 接收一个字节的数据
    USART_ReceiveData(USART1);
    // 等待接收数据寄存器非空
    while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == RESET);
}

uint8_t serial_getflag(void)
{
    // 检查串口接收完成标志
    if(serial_rxflag == 1)
    {
        serial_rxflag =0; // 清除标志
        return 1;
    }
    return 0; // 没有接收完成
}

uint8_t serial_getrxdata(void)
{
    // 返回接收到的数据
    return serial_rxdata;
}

void USART1_IRQHandler(void)
{
    static uint8_t Rec_state_red = 0, m_red, n_red;  // 红色激光状态变量
    static uint8_t Rec_state_green = 0, m_green, n_green; // 绿色激光状态变量
    
    if (USART_GetFlagStatus(USART1, USART_IT_RXNE) == SET)
    {
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        uint8_t serial_rxdata = USART_ReceiveData(USART1);

        /* 红色激光坐标处理 */
        if (serial_rxdata == 0xFD)  {    
            Rec_state_red = 1;
        }
        else if (Rec_state_red == 1 && serial_rxdata != 0xFD){
            Rec_state_red = 2;
            m_red = serial_rxdata;
        }
        else if (Rec_state_red == 2 && serial_rxdata != m_red) {
            Rec_state_red = 3;
            n_red = serial_rxdata;
            Red_x = (m_red << 8) | n_red;
        }
        else if (Rec_state_red == 3 && serial_rxdata != n_red) {
            Rec_state_red = 4;
            m_red = serial_rxdata;
        }
        else if (Rec_state_red == 4 && serial_rxdata != m_red){
            Rec_state_red = 5;
            n_red = serial_rxdata;
            Red_y = (m_red << 8) | n_red;
        }
        else if (Rec_state_red == 5 && serial_rxdata == 0xFA) {
            Rec_state_red = 0;
            serial_rxflag = 1;
        }
        else if (Rec_state_red != 0) // 处理异常情况 
		{
            Rec_state_red = 0;
        }

        /* 绿色激光坐标处理 */
        if (serial_rxdata == 0xAB) // 帧头
		{    
            Rec_state_green = 1;
        }
        else if (Rec_state_green == 1 && serial_rxdata != 0xAB){
            Rec_state_green = 2;
            m_green = serial_rxdata;
        }
        else if (Rec_state_green == 2 && serial_rxdata != m_green){
            Rec_state_green = 3;
            n_green = serial_rxdata;
            Green_x = (m_green << 8) | n_green;
        }
        else if (Rec_state_green == 3 && serial_rxdata != n_green){
            Rec_state_green = 4;
            m_green = serial_rxdata;
        }
        else if (Rec_state_green == 4 && serial_rxdata != m_green){
            Rec_state_green = 5;
            n_green = serial_rxdata;
            Green_y = (m_green << 8) | n_green;
        }
        else if (Rec_state_green == 5 && serial_rxdata == 0xCD) // 帧尾
        {
            Rec_state_green = 0;
            serial_rxflag = 1;
        }
        else if (Rec_state_green != 0) // 处理异常情况
        {
            Rec_state_green = 0;
        }
    }
}

